package com.tuzhi.atuh.component;

import com.tuzhi.atuh.controller.http.AuthApiClient;
import com.tuzhi.auth.vo.LoginUser;
import com.tuzhi.base.web.util.ControllerUtils;
import com.tuzhi.common.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class AuthUser {

    @Autowired
    private AuthApiClient authApiClient;
    /**
     * 通过token 调用 auth系统获取 当前登录用户的基础信息
     *
     * @param isNullThrow true,当null 是，抛出异常，false直接返回 null
     * @return
     */
    public LoginUser getCurLoginUser4Token(boolean isNullThrow) {
        LoginUser loginUser = authApiClient.getCurUser(ControllerUtils.getToken(null));
        if (loginUser == null) {
            if (isNullThrow) {
                throw new BusinessException("获取不到登录用户信息");
            }
        }
        return loginUser;
    }
}
